<template>
  <van-button
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="isFollowLoading"
    v-if="!isFollowed"
    @click="followUser"
  >关注</van-button>
  <van-button
    v-else
    class="follow-btn"
    round
    size="small"
    @click="followUser"
    :loading="isFollowLoading"
  >已关注</van-button>
</template>

<script>
import { removeFollowUser, addFollowUser } from '@/api/user.js'
export default {
  data() {
    return {
      isFollowLoading: false
    }
  },
  // 定义v-model的解析规则
  model: {
    event: 'update-is-followed', // 事件就会解析成 update-is-followed
    prop: 'isFollowed' // 传递的属性自定义
  },

  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    autId: {
      type: [Object, Number],
      required: true
    }
  },

  created() {
  },

  methods: {
    // 点击关注或已关注按钮
    async followUser() {
      this.isFollowLoading = true
      try {
        if (this.isFollowed) {
          await removeFollowUser(this.autId)
        } else {
          await addFollowUser(this.autId)
        }
        this.$emit('update-is-followed', !this.isFollowed)
        this.$toast('操作成功')
      } catch (err) {
        this.$toast('操作失败')
      }
      this.isFollowLoading = false
    }
  }
}
</script>

<style scoped lang='less'>
</style>
